import Vue from 'vue'
import VueRouter, {RouteConfig} from 'vue-router'
import storage from '../utils/storage'
import main from '../views/main.vue'
import request from "@/views/request.vue";

Vue.use(VueRouter)
export const loadView = (view: any) => {
    return (resolve: any) => require([`@/views/${view}.vue`], resolve)
}
const routes: Array<RouteConfig> = [
    {
        path: '/login',
        name: 'login',
        component: loadView('login')
    },
    {
        path: '/NewTravelAllowanceRequest/:id',
        redirect: '/NewTravelAllowanceRequest/:id',
        name: "requestMain",
        component: request,
        children: [{
            path: '/NewTravelAllowanceRequest/:id',
            name: "new_travel_allowance_request",
            component: loadView('newtravelallowance/newtravelallowance')
        }]
    },
    {
        path: '/',
        name: 'main',
        component: main,
        children: [{
            path: 'MyClaims',
            name: "default_defaultpage",
            component: loadView('default/defaultpage')
        }, {
            path: 'FinanceOperations',
            name: "finance_operations",
            component: loadView('FinanceOperation/FinanceOperations')
        }, {
            path: 'SystemConfiguration',
            name: "system_configuration",
            component: loadView('SystemConfiguration/SystemConfiguration')
        }, {
            path: 'FinanceOperations',
            name: "finance_operations",
            component: loadView('FinanceOperation/FinanceOperations')
        }, {
            path: 'SystemConfiguration',
            name: "system_configuration",
            component: loadView('SystemConfiguration/SystemConfiguration')
        }, {
            path: 'MaintainTerritory',
            name: "maintain_territory",
            component: loadView('SystemConfiguration/MaintainTerritory')
        }]
    }
]

const router = new VueRouter({
    mode: "history",
    routes
})
router.beforeEach(async (to, from, next) => {
    if (to.meta.notAuth) {
        return next();
    }
    var token = storage.get("token");
    if (to.name !== 'login' && !token)
        return next({name: 'login'})
    return next();

    // setTimeout(() => {
    //     this.$router.push({
    //         path: "/SecurityVerify"
    //     });
    // }, 2000);

});

export default router
